package com.minshenglife.zanzan.web.service;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * ������ɷ��������service
 * @author wll
 *	2013-4-8
 */
public class RequestTaskService {
	
	private final String LOG = RequestTaskService.class.toString();

	public static String sendDataStream(String uploadUrl,String data) throws Exception {

		
		String CHARSET = "UTF-8";
		OutputStream outputStream = null;
		InputStream inputStream = null;

		String end = "\r\n", result, twoHyphens = "--", boundary = "******";
		URL url = new URL(uploadUrl);
		HttpURLConnection httpURLConnection = (HttpURLConnection) url
				.openConnection();
		//���ó�ʱʱ��20s
		httpURLConnection.setConnectTimeout(1000*20);
		
		httpURLConnection.setDoInput(true);
		httpURLConnection.setDoOutput(true);
		httpURLConnection.setUseCaches(false);
		httpURLConnection.setRequestMethod("POST");
		httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
		httpURLConnection.setRequestProperty("Charset", CHARSET);
		httpURLConnection.setRequestProperty("Content-Type",
				"multipart/form-data;boundary=" + boundary);

//		StringBuilder sb = new StringBuilder();
//
//		for (Map.Entry<String, String> entry : params.entrySet()) {
//
//
//			sb.append(twoHyphens);
//			sb.append(boundary);
//			sb.append(end);
//			sb.append("Content-Disposition: form-data; name=\""
//					+ entry.getKey() + "\"" + end);
//			sb.append("Content-Type: text/plain; charset=" + CHARSET + end);
//			sb.append("Content-Transfer-Encoding: 8bit" + end);
//			sb.append(end);
//			sb.append(entry.getValue());
//			sb.append(end);
//		}

		try {

			if(data!=null && !"".equals(data)){
			outputStream = new DataOutputStream(
					httpURLConnection.getOutputStream());

	//			outputStream.write(sb.toString().getBytes(CHARSET));
				outputStream.write(data.getBytes(CHARSET));
				outputStream.flush();
			}
			inputStream = httpURLConnection.getInputStream();
			InputStreamReader isr = new InputStreamReader(inputStream, CHARSET);
			BufferedReader br = new BufferedReader(isr);
			result = br.readLine();

		} finally {
			if (outputStream != null) {
				outputStream.close();
			}
			if (inputStream != null) {
				inputStream.close();
			}
		}
		return result;
	}
	
public static InputStream sendDataStream2(String uploadUrl,String data) throws Exception {

		
		String CHARSET = "UTF-8";
		OutputStream outputStream = null;
		InputStream inputStream = null;

		String end = "\r\n", result, twoHyphens = "--", boundary = "******";
		URL url = new URL(uploadUrl);
		HttpURLConnection httpURLConnection = (HttpURLConnection) url
				.openConnection();
		//���ó�ʱʱ��20s
		httpURLConnection.setConnectTimeout(1000*20);
		
		httpURLConnection.setDoInput(true);
		httpURLConnection.setDoOutput(true);
		httpURLConnection.setUseCaches(false);
		httpURLConnection.setRequestMethod("POST");
		httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
		httpURLConnection.setRequestProperty("Charset", CHARSET);
		httpURLConnection.setRequestProperty("Content-Type",
				"multipart/form-data;boundary=" + boundary);

//		StringBuilder sb = new StringBuilder();
//
//		for (Map.Entry<String, String> entry : params.entrySet()) {
//
//
//			sb.append(twoHyphens);
//			sb.append(boundary);
//			sb.append(end);
//			sb.append("Content-Disposition: form-data; name=\""
//					+ entry.getKey() + "\"" + end);
//			sb.append("Content-Type: text/plain; charset=" + CHARSET + end);
//			sb.append("Content-Transfer-Encoding: 8bit" + end);
//			sb.append(end);
//			sb.append(entry.getValue());
//			sb.append(end);
//		}

		try {

			outputStream = new DataOutputStream(
					httpURLConnection.getOutputStream());

//			outputStream.write(sb.toString().getBytes(CHARSET));
			outputStream.write(data.getBytes(CHARSET));
			outputStream.flush();

			inputStream = httpURLConnection.getInputStream();

		} finally {
			if (outputStream != null) {
				outputStream.close();
			}
			if (inputStream != null) {
				inputStream.close();
			}
		}
		return inputStream;
	}
}
